package serializer

import (
	"singo/model"
	"strings"
	"time"
)

// Task 序列化器
type Task struct {
	UID          string        `json:"id"`
	Name         string        `json:"name"`
	Intro        string        `json:"intro"`
	Status       uint          `json:"status"`
	Priority     uint          `json:"priority"`
	Progress     int           `json:"progress"`
	Weight       float64       `json:"weight"`
	Score        float64       `json:"score"`
	CreatedBy    interface{}   `json:"created_by"`
	ReviewBy     interface{}   `json:"review_by"`
	ExecuteBy    []interface{} `json:"execute_by"`
	Project      interface{}   `json:"project"`
	ReviewStatus uint          `json:"review_status"`
	CreatedAt    string        `json:"created_at"`
	StartAt      string        `json:"start_at"`
	UpdatedAt    string        `json:"updated_at"`
	DeadlineAt   string        `json:"deadline_at"`
	CompleteAt   string        `json:"complete_at"`
}

// TaskSimple 序列化器
type TaskSimple struct {
	UID          string        `json:"id"`
	Name         string        `json:"name"`
	Intro        string        `json:"intro"`
	Status       uint          `json:"status"`
	Priority     uint          `json:"priority"`
	Progress     int           `json:"progress"`
	Weight       float64       `json:"weight"`
	Score        float64       `json:"score"`
	ReviewBy     interface{}   `json:"review_by"`
	ExecuteBy    []interface{} `json:"execute_by"`
	CreatedBy    interface{}   `json:"created_by"`
	Project      interface{}   `json:"project"`
	CreatedAt    string        `json:"created_at"`
	ReviewStatus uint          `json:"review_status"`
	StartAt      string        `json:"start_at"`
	UpdatedAt    string        `json:"updated_at"`
	DeadlineAt   string        `json:"deadline_at"`
	CompleteAt   string        `json:"complete_at"`
}

// BuildTask 序列化
func BuildTask(item model.Task) Task {
	var executeUsers []interface{}
	executeArr := strings.Split(item.ExecuteBy, ",")
	for _, item := range executeArr {
		executeUsers = append(executeUsers, BuildUserSimple(item))
	}

	return Task{
		UID:       item.UID,
		Name:      item.Name,
		Intro:     item.Intro,
		Status:    item.Status,
		Priority:  item.Priority,
		Project:   BuildProjectSimple(item.ProjectID),
		Progress:  item.Progress,
		Weight:    item.Weight,
		Score:     item.Score,
		ReviewBy:  BuildUserSimple(item.ReviewBy),
		CreatedBy: BuildUserSimple(item.CreatedBy),
		// CreatedBy:    item.CreatedBy,
		ExecuteBy:    executeUsers,
		CreatedAt:    item.CreatedAt.In(time.Local).Format("2006-01-02 15:04:05"),
		UpdatedAt:    item.UpdatedAt.In(time.Local).Format("2006-01-02 15:04:05"),
		ReviewStatus: item.ReviewStatus,
		StartAt:      serializerTime(item.StartAt),
		DeadlineAt:   serializerTime(item.DeadlineAt),
		CompleteAt:   serializerTime(item.CompleteAt),
	}
}

// BuildTaskSimple 序列化
func BuildTaskSimple(item model.Task) TaskSimple {
	var executeUsers []interface{}
	if item.ExecuteBy != "" {
		executeArr := strings.Split(item.ExecuteBy, ",")
		for _, item := range executeArr {
			executeUsers = append(executeUsers, BuildUserSimple(item))
		}
	} else {
		executeUsers = []interface{}{}
	}

	return TaskSimple{
		UID:          item.UID,
		Name:         item.Name,
		Intro:        item.Intro,
		Status:       item.Status,
		Priority:     item.Priority,
		Project:      BuildProjectSimple(item.ProjectID),
		Progress:     item.Progress,
		Weight:       item.Weight,
		ReviewBy:     BuildUserSimple(item.ReviewBy),
		Score:        item.Score,
		CreatedBy:    item.CreatedBy,
		ExecuteBy:    executeUsers,
		CreatedAt:    item.CreatedAt.In(time.Local).Format("2006-01-02 15:04:05"),
		UpdatedAt:    item.UpdatedAt.In(time.Local).Format("2006-01-02 15:04:05"),
		ReviewStatus: item.ReviewStatus,
		StartAt:      serializerTime(item.StartAt),
		DeadlineAt:   serializerTime(item.DeadlineAt),
		CompleteAt:   serializerTime(item.CompleteAt),
	}
}

// BuildTasks 序列化列表
func BuildTasks(items []model.Task) (tasks []Task) {
	for _, item := range items {
		Task := BuildTask(item)
		tasks = append(tasks, Task)
	}
	return tasks
}

// BuildTasksSimple 序列化列表
func BuildTasksSimple(items []model.Task) (tasks []TaskSimple) {
	for _, item := range items {
		Task := BuildTaskSimple(item)
		tasks = append(tasks, Task)
	}
	return tasks
}
